package com.example.data03.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.data03.entity.Count;
import com.example.data03.service.CountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/count")
public class CountController {
    @Autowired
    private CountService countService;

//    @RequestMapping("/")
//    public String toIndex(){
//        return "index";
//    }

    /**
     * 查询全部信息
     */
    @ResponseBody
    @RequestMapping("/get")
    public List<Count> getList(){
        QueryWrapper<Count> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("end");
        List<Count> getAll = countService.list(queryWrapper);
        return getAll;
    }


    /**
     * 写入countA countB
     */
    @ResponseBody
    @RequestMapping("/save")
    public QueryWrapper<Count> saveInfoList(double A, double B){
        QueryWrapper<Count> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", "1");
        Count count = new Count();
        count.setCounta(A);
        count.setCountb(B);
        count.setEnd(A+B);
        count.setPassEndA(A / count.getEnd());
        count.setPassEndB(B / count.getEnd());
        List<Count> list =new ArrayList<>();
        list.add(count);
        countService.update(count,queryWrapper);
        System.out.println("数据写入成功！");
        return queryWrapper;
    }
    /**
     * 刷新接口
     */
    @ResponseBody
    @RequestMapping("/update")
    public QueryWrapper<Count> updateInfo() {
        QueryWrapper<Count> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", "1");
        Count count = new Count();
        count.setCounta(0.0);
        count.setCountb(0.0);
        count.setEnd(0.0);
        count.setPassEndA(0.0);
        count.setPassEndB(0.0);
        countService.update(count,queryWrapper);
        System.out.println("接口刷新成功！");
        return queryWrapper;
    }
}
